<?php

namespace App\Hospital\Controllers;

use App\Admin\Actions\SetupSchedulingAction;
use App\Hospital\Repositories\HospitalAppoint;
use App\Models\AppointNum;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use App\Admin\Actions\Interval;
use App\Admin\Actions\VaccineIntervalAction;
use App\Admin\Forms\HospitalAppointIntervalForm;
use App\Admin\Forms\HospitalAppointMonthForm;
use App\Admin\Forms\HospitalAppointStreetForm;
use App\Admin\Grids\HospitalAppointVaccineGrid;
use App\Hospital\Controllers\AdminController;
use App\Hospital\Models\HospitalAppointModel;
use App\Models\HospitalAppoint as ModelsHospitalAppoint;
use App\Models\HospitalAppointMonth;
use App\Models\HospitalAppointStreet;
use App\Models\HospitalAppointType;
use App\Models\HospitalAppointVaccine;
use Dcat\Admin\Admin;
use Illuminate\Validation\Rule;

class HospitalAppointController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new HospitalAppointModel(), function (Grid $grid) {
            $grid->column('type')->using(HospitalAppointType::query()->pluck('name','id')->toArray());
            $grid->column('weeks')->display(function($model){
                return json_decode($this->weeks,true);
            })->map(function($value){
                $weeks = [
                    '1' => '周一  ',
                    '2' => '周二  ',
                    '3' => '周三  ',
                    '4' => '周四  ',
                    '5' => '周五  ',
                    '6' => '周六  ',
                    '0' => '周日  ',
                ];
                return $weeks[$value];
            })->badge();
            $grid->column('is_open')->switch('', true);


            $grid->column('interval','基础号源设置')->display('设置')->modal(function($model){

                return HospitalAppointIntervalForm::make()->payload(['hospital_appoint_id'=>$this->id,'hospital_id'=>$this->hospital_id]);
            });

            $grid->column('streen','街道设置')->display('设置')->modal(function($model){
                $street = HospitalAppointStreet::query()
                ->where('hospital_id',$this->hospital_id)
                ->where('hospital_appoint_id',$this->id)
                ->first();
                if(!$street){
                    $street = new HospitalAppointStreet();
                    $street->hospital_id = $this->hospital_id;
                    $street->hospital_appoint_id = $this->id;
                }
                return HospitalAppointStreetForm::make($street);
            });

            $grid->column('num','特殊号源/规则设置')
                ->if(function ($column) {
                    // 获取当前行其他字段值
                    $type = $this->type;

                    // $column->getValue() 是当前字段的值
                    // 返回 "真" 或 "假"，"真" 则执行 "if" 后面的代码
                    return in_array($type,[1,2,13]) === true;
                })
                ->display(function(){
                    if(in_array($this->type,[2,13])){
                        return "设置疫苗号源";
                    }elseif($this->type==1){
                        return "设置体检预约规则";
                    }
                    return "";
                })->modal(function ($modal) {
                    // 传递当前行字段值
                    if(in_array($this->type,[2,13])){
                        $modal->title('设置疫苗号源');
                        return HospitalAppointVaccineGrid::make(['hospital_appoint_id'=>$this->id,'hospital_id'=>$this->hospital_id]);
                    }elseif($this->type == 1){
                        $modal->title('设置体检预约规则');

                        $month = HospitalAppointMonth::query()
                        ->where('hospital_id',$this->hospital_id)
                        ->where('hospital_appoint_id',$this->id)
                        ->first();
                        if(!$month){
                            $month = new HospitalAppointMonth();
                            $month->hospital_id = $this->hospital_id;
                            $month->hospital_appoint_id = $this->id;
                        }
                        return HospitalAppointMonthForm::make($month);
                    }
                    return false;
                });


            // 也可以通过这种方式添加
            $grid->actions(function ($actions) {

                $actions->append(new SetupSchedulingAction());
                //$actions->append(new VaccineIntervalAction());

            });
            //$grid->setActionClass(Grid\Displayers\Actions::class);

            //$grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableViewButton();
            $grid->disableQuickEditButton();
            $grid->disableRowSelector();
            $grid->disableFilterButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HospitalAppoint(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('cycle');
            $show->field('delay');
            $show->field('hospital_id');
            $show->field('sure_date');
            $show->field('non_date');
            $show->field('weeks');
            $show->field('intro');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(HospitalAppointModel::with('intervals'), function (Form $form) {
            $types=HospitalAppointType::query()->pluck('name','id')->toArray();

            $form->select('type')->options($types)->rules(

                    'required|unique:eb_hospital_appoint,type,'.$form->model()->id.',id,hospital_id,'.$form->model()->hospital_id

            )->when(1, function (Form $form) {
                $form->switch('is_month','是否开启体检月龄限制')
                    ->saving(function ($v) {
                        return $v ? 1 : 0;
                    });
            });
            $form->select('release_time','放号时间')->options([
                '00:00'=>'00:00',
                '05:00'=>'05:00',
                '06:00'=>'06:00',
                '07:00'=>'07:00',
                '11:00'=>'11:00',
                '12:00'=>'12:00',
                '13:00'=>'13:00',
                '14:00'=>'14:00',
                '15:00'=>'15:00',
                '16:00'=>'16:00',
                '17:00'=>'17:00',
                '18:00'=>'18:00',
                '19:00'=>'19:00',
                '20:00'=>'20:00',
                '21:00'=>'21:00',
                '22:00'=>'22:00',
                '23:00'=>'23:00',
            ])->required();

            $form->number('cycle')->rules('required')->default(7);
            $form->number('delay')->rules('required');
            $form->number('extend_day','周期外可查看天数')->rules('required')->default(7);

            $form->checkbox('weeks')->options([
                '1' => '周一  ',
                '2' => '周二  ',
                '3' => '周三  ',
                '4' => '周四  ',
                '5' => '周五  ',
                '6' => '周六  ',
                '0' => '周日  ',
            ])->saving(function ($value) {
                return json_encode($value);
            });


            $sdate = strtotime(date('Y-m-01'));
            $edate = strtotime('+6 month', strtotime(date('Y-m-01')));
            $days = ($edate - $sdate) / 86400;
            for ($i = 0; $i < $days; $i++) {
                $time = strtotime('+' . $i . ' day', $sdate);
                $dayList[date('Y-m-d', $time)] = date('Y-m-d', $time);
            }
//            $form->multipleSelect("sure_date")->options($dayList)->saving(function ($value) {
//                return json_encode($value);
//            });
            $form->multipleSelect("non_date")->options($dayList)->saving(function ($value) {

                //清理相应排班
                AppointNum::query()->whereIn('date',$value)->delete();
                return json_encode($value);
            });
            $form->textarea('intro')->rules('required');
            $hospital_id = Admin::user()->hospital_id;

            $form->hasMany('intervals', function (Form\NestedForm $form) use($hospital_id) {
                $form->time('start')->format('HH:mm')->label('开始');
                $form->time('end')->format('HH:mm')->label('结束');
                $form->hidden('hospital_id')->value($hospital_id);
            })->label('时间段设置')->useTable();

            $form->switch('is_open')
            ->saving(function ($v) {
                return $v ? 1 : 0;
            });

            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableViewCheck();

            $form->display('created_at');
            $form->display('updated_at');



        });
    }
}
